package com.myedu.draw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.EventListener;

/**
 * @author Little fat D
 * @version 1.0
 */
public class MoveBall extends JFrame {
    private BallPanel bp = null;

    public static void main(String[] args) {
        new MoveBall();
    }

    public MoveBall() {
        bp = new BallPanel();
        this.add(bp);
        this.setSize(600, 400);
        this.addKeyListener(bp);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

class BallPanel extends JPanel implements KeyListener {
    int x = 0;
    int y = 0;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //g.fillOval(x, y, 30, 30);
        g.fill3DRect(x, y, 10, 60, false);
        g.fill3DRect(x + 30, y, 10, 60, false);
        g.fill3DRect(x + 10, y + 10, 20, 40, false);
        g.fillOval(x + 10, y + 20, 20, 20);
        g.drawLine(x + 20, y + 20, x + 20, y);
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            y--;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            y++;
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            x--;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            x++;
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
